package dk.hojlind.hackhunt.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import dk.hojlind.hackhunt.control.AdvisoryHandler;
import dk.hojlind.hackhunt.control.LogHandler;
import dk.hojlind.hackhunt.control.MachineHandler;
import dk.hojlind.hackhunt.control.PlayerHandler;
import dk.hojlind.hackhunt.control.RegistryHandler;
import dk.hojlind.hackhunt.model.IPGetter;
import dk.hojlind.hackhunt.model.Machine;
import dk.hojlind.hackhunt.model.Observer;
import dk.hojlind.hackhunt.model.Subject;

//GUI creation class
public class HackHuntGUI extends JFrame implements Observer {

	//Public variables

	//Private variables
	String adress = "dragon-online.net";
	int port = 1337;
	DefaultListModel<String> playerItems;
	final JFrame frame = new JFrame();
	ArrayList<String> players;
	ArrayList<String> ipAddresses = new ArrayList<String>();
	Timer timer;

	//Handlers
	RegistryHandler registryHandler;
	LogHandler logHandler;
	PlayerHandler playerHandler;
	AdvisoryHandler advisoryHandler;
	MachineHandler machineHandler;

	//Models
	IPGetter ipGetter;
	Machine machine;
	
	//Constructor
	public HackHuntGUI(Subject serverService)
	{
		registryHandler = new RegistryHandler(adress, port);
		logHandler = new LogHandler(adress, port);
		playerHandler = new PlayerHandler(adress, port);
		advisoryHandler = new AdvisoryHandler(adress, port, serverService);
		machineHandler = new MachineHandler(adress, port, serverService, machine);
		playerItems = new DefaultListModel<String>();
		timer = new Timer();
		ipGetter = new IPGetter();
		InitGUI();

	}

	//Setup the GUI
	private void InitGUI()
	{
		//Create panel
		JPanel panel = new JPanel();
		getContentPane().add(panel);

		//Called to position components absolutely
		panel.setLayout(null);

		//Create label to inform player of stuff
		final JLabel label = new JLabel("This is totally working");
		label.setBounds(190, 170, 150, 30);

		//Create Username textfield and place it
		final JTextField username = new JTextField("Username");
		username.setBounds(190, 20, 150, 30);
		username.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e)
			{
				if(username.getText().equals("Username"))
				{
					username.setText("");
				}
			}
			@Override
			public void focusLost(FocusEvent e){}
		});

		//Create password textfield and place it
		final JPasswordField password = new JPasswordField("password");
		password.setBounds(190, 70, 150, 30);
		password.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e)
			{
				if(password.getText().equals("password"))
				{
					password.setText("");
				}
			}
			@Override
			public void focusLost(FocusEvent e){}
		});

		//Create IP field and place it
		final JComboBox address = new JComboBox();
		address.setBounds(190, 120, 150, 30);
		ipAddresses = (ArrayList<String>) ipGetter.getIp();
		for(int i = 0; i < ipAddresses.size(); i++)
		{
			address.addItem(ipAddresses.get(i));
		}

		//Create a list to store the active players and place it
		JList<String> playerList = new JList<String>(playerItems);
		playerList.setBounds(360, 20, 400, 200);
		JScrollPane scrollpane = new JScrollPane(playerList);

		//Create quit button and assign text as well as place it
		JButton quit = new JButton("Quit");
		quit.setBounds(20, 20, 150, 30);
		quit.setToolTipText("Terminates the program");

		//Add listener which reacts when the  quit button is clicked
		quit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});

		//Create register button and assign text as well as place it
		JButton register = new JButton("Register");
		register.setBounds(20, 70, 150, 30);
		register.setToolTipText("Register for HackHunt");

		//Add listener which reacts when the register button is clicked
		register.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {

				//Create dialog for registration
				RegistryDialog register = new RegistryDialog(registryHandler);
				register.setVisible(true);
			}
		});

		//Create delete button and assign text as well as place it
		JButton delete = new JButton("Delete");
		delete.setBounds(20, 120, 150, 30);
		delete.setToolTipText("Register for HackHunt");

		//Add listener which reacts when the delete button is clicked
		delete.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {

				//Create dialog for deletion
				DeleteDialog delete = new DeleteDialog(registryHandler);
				delete.setVisible(true);
			}
		});

		//Create log in button and assign text as well as place it
		JButton logIn = new JButton("Log In");
		logIn.setBounds(20, 170, 150, 30);
		logIn.setToolTipText("Log in to your account");

		//Add listener which reacts when the log in button is clicked
		logIn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Thread t1 = new Thread(new Runnable() {

					@Override
					public void run() {
						//Set text of label with whatever is returned from the login
						//PASSWORD HANDLING MUST BE CHANGED
						label.setText(logHandler.logIn(username.getText(), password.getText(), address.getSelectedItem().toString()));				
						advisoryHandler.setUser(username.getText());
						advisoryHandler.setPass(password.getText());
						advisoryHandler.setOwnAddress(address.getSelectedItem().toString());
						
					}

				});

				t1.start();


			}
		});

		//Create log out button and assign text as well as place it
		JButton logOut = new JButton("Log Out");
		logOut.setBounds(20, 220, 150, 30);
		logOut.setToolTipText("Log out of your account");

		//Add listener which reacts when the log out button is clicked
		logOut.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Thread t1 = new Thread(new Runnable() {

					@Override
					public void run() {
						//PASSWORD HANDLING MUST BE CHANGED
						label.setText(logHandler.logOut(username.getText(), password.getText()));				
					}

				});

				t1.start();

			}
		});

		//Create showplayers button and assign text as well as place it
		JButton showPlayers = new JButton("Show active players");
		showPlayers.setBounds(20, 270, 150, 30);
		showPlayers.setToolTipText("Show currently active players");

		//Add listener which reacts when the showplayers button is clicked
		showPlayers.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{
				Thread t1 = new Thread(new Runnable() {

					@Override
					public void run() {
						//Get arraylist of players which is then run through and added to playeritems which is used by the JList to show all the different items
						players = playerHandler.getActivePlayers();
						playerItems.clear();
						for(int i = 0; i < players.size(); i++)
						{
							playerItems.addElement(players.get(i));
						}	
					}

				});

				t1.start();
			}
		});

		//Create quit button and assign text as well as place it
		JButton advisories = new JButton("Advisories");
		advisories.setBounds(20, 320, 150, 30);
		advisories.setToolTipText("View advisories");

		//Add listener which reacts when the  quit button is clicked
		advisories.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				AdvisoryDialog advisories = new AdvisoryDialog(advisoryHandler);
				advisories.setVisible(true);
			}
		});

		


		//Add buttons to panel to show the button
		panel.add(quit);
		panel.add(register);
		panel.add(delete);
		panel.add(logIn);
		panel.add(logOut);
		panel.add(showPlayers);
		panel.add(username);
		panel.add(password);
		panel.add(label);
		panel.add(playerList);
		panel.add(address);
		panel.add(advisories);
		panel.add(scrollpane);

		setTitle("Hack Hunt Client");
		setSize(800, 450);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		//Set timer which autoupdates the active users each 15 seconds
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {

				players = playerHandler.getActivePlayers();
				playerItems.clear();
				for(int i = 0; i < players.size(); i++)
				{
					playerItems.add(i, players.get(i));
				}
			}
		}, 15*1000, 15*1000);

	}

	@Override
	public void update(String message) {
		System.out.println(message);
	}

}
